﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArch.Utils;

namespace YJKArch.AxisModule
{
	public partial class WallToGridForm : System.Windows.Forms.Form
	{
		public WallToGridForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.InitializeGridStyle();
		}

		public GridType GetGridType()
		{
			if (this.m_GridTypeList.Count == 0)
			{
				return null;
			}
			return this.m_GridTypeList[this.cmbGridType.SelectedIndex];
		}

		public double ExtendLength()
		{
			return AssistFunc.mmToFeet((double)Convert.ToInt32(this.tbGridExtend.Text));
		}

		private void InitializeGridStyle()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(GridType)))
			{
				GridType gridType = element as GridType;
				if (gridType != null && gridType.Name.IndexOf('_') < 0)
				{
					this.cmbGridType.Items.Add(gridType.Name);
					this.m_GridTypeList.Add(gridType);
				}
			}
			if (this.cmbGridType.Items.Count > 0)
			{
				this.cmbGridType.SelectedIndex = 0;
			}
		}

		private void WallToGridForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void WallToGridForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			this.SaveCompornentData();
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + "WallToGridForm.dat";
			if (!File.Exists(path))
			{
				this.m_GridExtend = "1000";
				this.tbGridExtend.Text = this.m_GridExtend;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num >= this.m_GridTypeList.Count)
			{
				num = 0;
			}
			this.cmbGridType.SelectedIndex = num;
			this.m_GridExtend = streamReader.ReadLine();
			this.tbGridExtend.Text = this.m_GridExtend;
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "WallToGridForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbGridType.SelectedIndex);
			streamWriter.WriteLine(this.tbGridExtend.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void tbGridExtend_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbGridExtend.Text, 0.0, 10000.0))
			{
				this.tbGridExtend.Text = this.m_GridExtend;
				MessageBox.Show("合法范围在0mm～10000mm之间", "盈建科提示");
				return;
			}
			this.m_GridExtend = this.tbGridExtend.Text;
		}

		private ExternalCommandData m_Revit;

		private List<GridType> m_GridTypeList = new List<GridType>();

		private string m_GridExtend = "";
	}
}
